package com.simen.downloader;

import android.os.Parcel;
import android.os.Parcelable;

import com.simen.downloader.model.Filer;
import com.simen.downloader.util.Constants;
import com.simen.downloader.util.Utils;

import java.io.Serializable;
import java.net.URL;

/**
 * Created by zhangming on 16/5/12.
 */
final class ParcelFile implements Filer, Parcelable {

    private String fileUrl;
    private String fileRealUrl;
    private long current, total;
    private int progress;
    private long lasttime, createtime;
    private int status; //任务完成状态
    private String fileName, localPath;

    ParcelFile() {

    }

    public ParcelFile(String fileUrl, String fileRealUrl, long createtime,
                      long lasttime, long current, long total, int progress, String fileName,
                      String localPath) {
        this.createtime = createtime;
        this.current = current;
        this.fileName = fileName;
        this.fileRealUrl = fileRealUrl;
        this.fileUrl = fileUrl;
        this.lasttime = lasttime;
        this.progress = progress;
        this.total = total;
        this.localPath = localPath;
    }

    public ParcelFile(String fileUrl, int status) {
        this.fileUrl = fileUrl;
        this.status = status;
    }

    public ParcelFile(StorageFile storageFile) {
        this.createtime = storageFile.getCreatetime();
        this.current = storageFile.getCurrent();
        this.fileName = storageFile.getFileName();
        this.fileRealUrl = storageFile.getFileRealUrl();
        this.fileUrl = storageFile.getFileUrl();
        this.lasttime = storageFile.getLasttime();
        this.progress = storageFile.getProgress();
        this.total = storageFile.getTotal();
        this.localPath = storageFile.getLocalPath();
    }

    public long getCreatetime() {
        return createtime;
    }

    public void setCreatetime(long createtime) {
        this.createtime = createtime;
    }

    public long getCurrent() {
        return current;
    }

    public void setCurrent(long current) {
        this.current = current;
    }

    public String getExt() {
        return Utils.getExtensionName(fileName);
    }

    public String getFileRealUrl() {
        return fileRealUrl;
    }

    public void setFileRealUrl(String fileRealUrl) {
        this.fileRealUrl = fileRealUrl;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public long getLasttime() {
        return lasttime;
    }

    public void setLasttime(long lasttime) {
        this.lasttime = lasttime;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    boolean isCleared() {
        if (status == Constants.WORKCLEARED) {
            return true;
        }
        return false;
    }

    public boolean isEqualFileUrl(URL finalUrl) {
        if (finalUrl == null || this.fileUrl == null) {
            return false;
        }

        if (this.fileUrl.equals(finalUrl.toString())) {
            return true;
        }

        return false;
    }

    public boolean isCompleted() {
        return total > 0 && current == total;
    }

    @Override
    public String toString() {
        return "StorageFile{" +
                "createtime=" + createtime +
                ", fileUrl='" + fileUrl + '\'' +
                ", fileRealUrl='" + fileRealUrl + '\'' +
                ", current=" + current +
                ", total=" + total +
                ", progress=" + progress +
                ", lasttime=" + lasttime +
                ", status=" + status +
                ", fileName='" + fileName + '\'' +
                ", localPath='" + localPath + '\'' +
                '}';
    }

    public void setParamers(String key, String fileRealUrl, long createtime, long lasttime, long current, long total, int progress, String fileName, String filePath) {
        this.createtime = createtime;
        this.current = current;
        this.fileName = fileName;
        this.fileRealUrl = fileRealUrl;
        this.fileUrl = key;
        this.lasttime = lasttime;
        this.progress = progress;
        this.total = total;
        this.localPath = filePath;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.fileUrl);
        dest.writeString(this.fileRealUrl);
        dest.writeLong(this.current);
        dest.writeLong(this.total);
        dest.writeInt(this.progress);
        dest.writeLong(this.lasttime);
        dest.writeLong(this.createtime);
        dest.writeInt(this.status);
        dest.writeString(this.fileName);
        dest.writeString(this.localPath);
    }

    protected ParcelFile(Parcel in) {
        this.fileUrl = in.readString();
        this.fileRealUrl = in.readString();
        this.current = in.readLong();
        this.total = in.readLong();
        this.progress = in.readInt();
        this.lasttime = in.readLong();
        this.createtime = in.readLong();
        this.status = in.readInt();
        this.fileName = in.readString();
        this.localPath = in.readString();
    }

    public static final Parcelable.Creator<ParcelFile> CREATOR = new Parcelable.Creator<ParcelFile>() {
        @Override
        public ParcelFile createFromParcel(Parcel source) {
            return new ParcelFile(source);
        }

        @Override
        public ParcelFile[] newArray(int size) {
            return new ParcelFile[size];
        }
    };
}
